package com.turkline.CatchMePolygon;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import aurelienribon.tweenengine.equations.Quad;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.impl.GLScreen;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.Vector2;

public class LevelSelectScreen extends GLScreen 
{
	private float stateTime = 0;
	Help help;
	private TouchEvent e;
	
	public LevelSelectScreen(Game game, int maxSprites, int camWidth, int camHeight) {
		super(game, maxSprites, camWidth, camHeight);
		Settings.showHelpStartScreen = false;
		// TODO Auto-generated constructor stub
		
		
	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> events = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		if(Settings.showHelpStartScreen)
		{
			help.update(deltaTime, e);
		}
		for(int i = 0 ; i< events.size() ; i++)
		{
			e = events.get(i);
			Vector2 tPoint = new Vector2(e.x, 480-e.y);
			if(e.type == TouchEvent.TOUCH_UP) {
				/*if(OverlapTester.pointInRectangle(Assets.toMainMenu, tPoint)) {
					game.setScreen(new StartScreen(game, 50, 800, 480));
				}*/
				
				if(OverlapTester.pointInRectangle(Assets.soundButton, tPoint) && !Settings.showHelpStartScreen)
				{
					Settings.soundEnabled = !Settings.soundEnabled;
				}
				if(OverlapTester.pointInRectangle(Assets.helpButton, tPoint) && !Settings.showHelpStartScreen)
				{
					help = new Help(glGraphics, batcher, game);
					Settings.showHelpStartScreen = true;
				}
				
				for(int k = 0; k < 20; k++){
					if(Settings.unlockedLevels[k] == 1) {
						if(OverlapTester.pointInRectangle(Assets.levelNumRect[k], tPoint) && !Settings.showHelpStartScreen) {
							Settings.CURRENT_LEVEL = k+1;
							Assets.playSound(Assets.clickSound);
							game.setScreen(new GameScreen(game, 2000, 800, 480, k+1) , false);
						}
					}
				}
				//eger bolum kilitli ise overlap tester olmaz ve buton pasif olur
			}
		}
	}

	
	
	@Override
	public void present(float deltaTime) 
	{
		stateTime +=deltaTime;
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		camera.setViewportAndMatrices();
		
		batcher.beginBatch(Assets.items);
		batcher.drawSprite(400, 240, 800, 480, 1, Assets.backRegion);
		batcher.drawSprite(400, 335, Assets.logoRegion.width, Assets.logoRegion.height, 0.2f, Assets.logoRegion);
		batcher.drawSprite(400, 29, Assets.controlButtonsRegion.width, Assets.controlButtonsRegion.height, 0.7f, Assets.controlButtonsRegion);
		batcher.drawSprite(363, 25, Assets.controlButtonSoundRegion.width, Assets.controlButtonSoundRegion.height, 1, Settings.soundEnabled ? Assets.controlButtonSoundRegion : Assets.controlButtonSoundOffRegion);
		batcher.drawSprite(442, 25, Assets.controlButtonHelpRegion.width, Assets.controlButtonHelpRegion.height, 1, Assets.controlButtonHelpRegion);
		for(int i = 0; i < 20; i++) {
			int iy = i/10;
			batcher.drawSprite(((i%10)*78)+49, 360-(i/10)*95, 64, 64, 1, Assets.levelSelectSqRegion);
			
			if(Settings.levelScores[i] < 60) {
				//bkz. basini almak
			} else if(Settings.levelScores[i] >= 90) {
				batcher.drawSprite(((i%10)*78)+49, 360-iy*95 - 30, 32, 64, 1, Assets.medalRegion[0]);
			} else if(Settings.levelScores[i] >= 75) {
				batcher.drawSprite(((i%10)*78)+49, 360-iy*95 - 30, 32, 64, 1, Assets.medalRegion[1]);
			} else if(Settings.levelScores[i] >= 60) {
				batcher.drawSprite(((i%10)*78)+49, 360-iy*95 - 30, 32, 64, 1, Assets.medalRegion[2]);
			}// settingsteki levelScores arrayindeki puanlara gore madalya koyuyoyru
			
			batcher.drawSprite(((i%10)*78)+49, 360-iy*95, 32, 32, 1, Assets.levelSelectNumRegion[i]);
			
			if(Settings.unlockedLevels[i] == 0) {
				batcher.drawSprite(((i%10)*78)+49, 360-iy*95-25, 32, 32, 1, Assets.lockedRegion);
			}// settingsteki unlockedLevels arrayindaki degerlere gore kilit koyanza
		}
		
		//batcher.drawSprite(400, 80, 256, 64, 1, Assets.toMainMenuRegion);
		batcher.endBatch();
		if(Settings.showHelpStartScreen)
		{
			help.present(deltaTime);
		}
		camera.entranceEffectZoom(Quad.OUT ,stateTime , 0.5f, 0.5f, 1f);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}
}